<?php

namespace Wechat\Controller;

class FinishedProductController extends CommonController {


    public function index() {

        $agent = check_access($this->customer["customer_id"], "0",null,1);
        $this->assign("access", check_access($this->customer["customer_id"], "0"));
        $this->display("index");
    }

    public function quickOrder() {
        $this->display("quickOrder");
    }

    public function quickOrderList() {
        $classId = I("post.classId");
        if(!isNullOrEmpty($classId)) {
            $map["p.category_id"] = $classId;
        }

        $list = D("Product")->alias("p")
            ->where($map)
//            ->field("p.product_id,p.product_name,c.is_finished")
            ->select();

        echo json_encode($list);

    }

    public function FinishedProductList() {

        $classId = I("post.classId");
        $keyword = I("post.keyword");

        if(!isNullOrEmpty($classId)) {
            $map["fp.class_id"] = $classId;
        }
        if(!isNullOrEmpty($keyword)) {
            $map["fp.name|fp.function|fp.indication|fp.supplier"] = array('like','%'.$keyword.'%');
        }

        $list = D("FinishedProduct")->alias("fp")
            ->join("left join __CLASS__ cl on cl.class_id = fp.class_id")
            ->order("fp.finished_product_id asc")
            ->where($map)
            ->field("fp.*,cl.class_name")
            ->select();

        echo json_encode($list);

    }

    public function finishedOrder() {
        if(!check_access($this->customer["customer_id"], "0")){
            $this->error("您没有访问权限！");
            exit();
        }
        $id = $_REQUEST["id"];
        $finishedProduct = D("FinishedProduct")->queryById($id);
//        if(isNullOrEmpty($finishedProduct)) {
//            $this->redirect(U("product/index"));
//        }
        $map["finished_product_id"] = $id;
        $agentList = D('Agent')
            ->where($map)
            ->select();
        $employee = D("Employee")->queryById($this->customer["customer_id"]);
        $agentId = $employee["agent_id"];

        $this->assign("finishedProduct", $finishedProduct);
        $this->assign("agentList", $agentList);
        $this->assign("agentId", $agentId);
        $this->display("finishedOrder");

    }

    public function createOrder() {

        $lastOrder = D("FinishedOrder")->alias("fo")
            ->order("fo.finished_order_id desc")
            ->limit(1)
            ->select();
        echo($lastOrder[0]["finished_order_no"]);

        $finishedProductId = $_REQUEST["finishedProductId"];
        $finishedProduct = D("FinishedProduct")->queryById($finishedProductId);
        $data["code"] = 200;

        if(isNullOrEmpty($finishedProductId)) {
            $data["code"] = -1;
            $data["msg"] = "商品不存在";
            echo json_encode($data);
            exit();
        }

        $employee = D("Employee")->queryById($this->customer["customer_id"]);
        $finishedOrderData["finished_order_no"] = "CD".date('ym',time()) . sprintf("%03d", substr(D("FinishedOrder")->count(), -3, 3)+1);
        $finishedOrderData["create_time"] = getCurrentDate();
        $finishedOrderData["finished_product_id"] = $finishedProductId;
        $finishedOrderData["sale_price"] = I("post.salePrice");
//        $finishedOrderData["size"] = I("post.size");
        $sizeNumList = explode('/',I("post.size"));
        $sizeNameList = explode('/',$finishedProduct["size"]);
        for($i=0; $i< count($sizeNumList); $i++){
            if($sizeNumList[$i] > 0){
                $finishedOrderData["size"] = $finishedOrderData["size"]. "[". $sizeNameList[$i] ."]x" .$sizeNumList[$i] ."，";
            }
        }

        $finishedOrderData["number"] = I("post.number");
        $finishedOrderData["delivery_type"] = I("post.deliveryType");
        $finishedOrderData["receive_name"] = I("post.receiveName");
        $finishedOrderData["receive_address"] = I("post.receiveAddress");
        $finishedOrderData["receive_contact"] = I("post.receiveContact");
        $finishedOrderData["customer_name"] = I("post.customerName");
        $finishedOrderData["patient_name"] = I("post.patientName");
        $finishedOrderData["patient_sex"] = I("post.patientSex");
        $finishedOrderData["patient_age"] = I("post.patientAge");
        $finishedOrderData["doctor_name"] = I("post.doctorName");
        $finishedOrderData["hospital_name"] = I("post.hospitalName");
        $finishedOrderData["note"] = I("post.note");
        $finishedOrderData["need_invoice"] = I("post.needInvoice");
        $finishedOrderData["invoice_name"] = I("post.invoiceName");
        $finishedOrderData["invoice_number"] = I("post.invoiceNumber");
        $finishedOrderData["invoice_detail"] = I("post.invoiceDetail");
        $finishedOrderData["agent_name"] = I("post.agentName");
        $finishedOrderData["total_price"] = $finishedOrderData["sale_price"]*$finishedOrderData["number"];
        $finishedOrderData["employee_id"] = $employee["employee_id"];
        $finishedOrderData["status"] = -1;

        $nameArr =array();
        array_push($nameArr, "finished_product_id");
        array_push($nameArr, "sale_price");
        array_push($nameArr, "number");
        array_push($nameArr, "delivery_type");
        array_push($nameArr, "receive_name");
        array_push($nameArr, "receive_address");
        array_push($nameArr, "receive_contact");
        array_push($nameArr, "customer_name");
        array_push($nameArr, "note");
        array_push($nameArr, "invoice_name");
        array_push($nameArr, "invoice_number");
        array_push($nameArr, "invoice_detail");
        array_push($nameArr, "agent_name");
        array_push($nameArr, "total_price");
        array_push($nameArr, "employee_id");

        $theSame = 1;
        foreach ($nameArr as $name) {
            if($finishedOrderData[$name] != $lastOrder[0][$name]){
                $theSame = -1;
            }
        }
        if($theSame == 1){
            $data["code"] = -1;
            $data["msg"] = "订单重复";
            echo json_encode($data);
            exit();
        }

        $finishedOrderId = D("FinishedOrder")->add($finishedOrderData);

//        $data["finished_order_id"] = $finishedOrderId;

        //推送信息到西钓客服
        $employeeList = D("Employee")->alias("e")
            ->join("left join __CUSTOMER__ c on c.customer_id = e.employee_id")
            ->field("e.*, c.openid")
            ->select();
        $openidArray = array();
        foreach ($employeeList as $singleEmployee) {
            $job = explode(',',$singleEmployee["job"]);
            if ($job[0] == "5" || $job[0] == "3" || $job[0] == "100") {//客服
                array_push($openidArray, $singleEmployee["openid"]);
            }
            if ($singleEmployee["employee_id"] == $employee["employee_id"] && !in_array($singleEmployee["openid"], $openidArray)) {
                array_push($openidArray, $singleEmployee["openid"]);
            }
        }
        foreach ($openidArray as $openid) {
            $notifyData["openid"] = $openid;
            $notifyData["url"] = "http://xwkang.com/wechat/finishedProduct/orderList/id/". $finishedOrderId;
            $notifyData["first"] = "收到成品订单。下单人：".$employee["employee_name"];
            $notifyData["keyword1"] = "工单号".$finishedOrderData["finished_order_no"];
            $notifyData["keyword2"] = $finishedProduct["name"]."(".$finishedOrderData["size"] .")共 ".$finishedOrderData["number"]." 件";
            $notifyData["keyword3"] = $finishedOrderData["create_time"];
            $notifyData["keyword4"] = $finishedOrderData["total_price"];
            $notifyData["keyword5"] = $finishedOrderData["delivery_type"];
            $notifyData["remark"] = "备注：".$finishedOrderData["note"];
            finishedOrderNotify($notifyData);
        }

        $data["code"] = 200;
        echo json_encode($data);

    }

    public function orderList() {
        $id = $_REQUEST["id"];
        $this->assign("orderId", $id);
        $this->display("orderList");
    }

    public function myOrderList() {
        $status = I("post.status");
        if(!isNullOrEmpty($status)) {
            $map["fo.status"] = $status;
        }
        $employee = D("Employee")->queryById($this->customer["customer_id"]);
        $job = explode(',',$employee["job"]);
        if($job[0] != 5 && $job[0] != 3 && $job[0] != 100){
            $map["fo.employee_id"] = $employee["employee_id"];
        }
        $orderId = I("post.orderId");
        if(!isNullOrEmpty($orderId)) {
            $map["finished_order_id"] = $orderId;
        }

        $list = D("FinishedOrder")->alias("fo")
            ->join("left join __FINISHED_PRODUCT__ fp on fp.finished_product_id = fo.finished_product_id")
            ->join("left join __EMPLOYEE__ e on e.employee_id = fo.employee_id")
            ->join("left join __ADMIN__ ad on ad.user_id = fo.delivery_id")
            ->where($map)
            ->order("fo.finished_order_id desc")
            ->field("fo.*,fp.price as single_price,fp.image_url,fp.name as finished_product_name, e.employee_name, ad.admin_name as delivery_name")
            ->select();

        echo json_encode($list);
    }
}